#include <iostream>

#include <wx/app.h>
#include <wx/debug.h>

#include "sqlite3.h"
#include "gpxdb.h"
#include "tileprovider.h"
#include "geoutils.h"
#include "httplib.h"

using namespace wildwood;

/*
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
    int i;
    for(i = 0; i < argc; i++)
    {
        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    printf("\n");
    return 0;
}
*/

void testGeoUtilsBasic()
{
    TilePos tp(0, 0, 0, 0, 0);
    wxASSERT(tp.isValid());

    tp.mZoom = 1;
    wxASSERT(tp.isValid());
    tp.mX = 1;
    wxASSERT(tp.isValid());
    tp.mX = 2;
    wxASSERT(!tp.isValid());

    TilePos tp2(0, 0, 220, 18, 0);
    std::cout << tp2 << std::endl;
    tp2.zoomIn();
    std::cout << tp2 << std::endl;
    tp2.zoomIn();
    std::cout << tp2 << std::endl;
    tp2.zoomOut();
    std::cout << tp2 << std::endl;
    tp2.zoomOut();
    std::cout << tp2 << std::endl;

    TilePos tp3(2, 4, 220, 18, 5);
    std::cout << tp3 << std::endl;
    tp3.move(10, 10);
    std::cout << tp3 << std::endl;
    tp3.move(310, -300);
    std::cout << tp3 << std::endl;

}

void testGeoUtils()
{
    GeoPos gp(0, 0);

    TilePos tp = GeoUtils::gpsPos2TilePos(gp, 0);
    wxASSERT(tp.mX == 0 && tp.mY == 0 && tp.mInX == 128 && tp.mInY == 128);

    gp.setLat(23.1111);
    gp.setLon(34.2222);
    tp = GeoUtils::gpsPos2TilePos(gp, 17);
    //std::cout << gp << " -> " << tp << std::endl;
    GeoPos gp2 = GeoUtils::tilePos2GeoPos(tp);
    //std::cout << tp << " -> " << gp2 << std::endl;
    wxASSERT(trunc(gp.getLon() * 100) == trunc(gp2.getLon() * 100));
    wxASSERT(trunc(gp.getLat() * 100) == trunc(gp2.getLat() * 100));
}

void testTileProvider()
{
    //TileProvider *mTileProvider = new TileProvider(wxT("OSM"), wxT("http://a.tile.openstreetmap.org/ZOOM/X/Y.png"), NULL, 0);
    TileProvider *mTileProvider = new TileProvider(wxT("OSM"), wxT("http://blue.pavoucek.cz/ZOOM/X/Y.png"), NULL, 0);

    TilePos tp = GeoUtils::gpsPos2TilePos(GeoPos(0, 0), 0);
    std::cout << "center pos: " << tp << std::endl;
    wxBitmap* tileBitmap = mTileProvider->getTileImage(tp);

    delete tileBitmap;
    delete mTileProvider;
}

void testHttpLib()
{
    //httplib::HttpRequest request(wxURL(wxT("http://blue.pavoucek.cz")));
    httplib::HttpRequest request(wxURL(wxT("http://a.tile.openstreetmap.org/0/0/0.png")));
    httplib::HttpLib h;
    httplib::HttpResponse *r = h.sendRequest(request);

    wxLogMessage(wxT("Response code: %d, body size: %d"), r->getStatusCode(), r->getBody().size());

    /*const std::vector<char> body = r->getBody();

    for (std::vector<char>::const_iterator it = body.begin() ; it != body.end(); ++it)
    {
        std::cout << *it;
    }
    std::cout << std::endl;*/

    delete r;
}

void testGpxDb()
{
  /*
    //GpxDb gpxDb;
    //gpxDb.loadGpx(wxT("/home/blue/in/test.gpx"));
    //gpxDb.loadGpx(wxT("c:\\blue\\gps\\20141231-210425.gpx"));

    char *zErrMsg = NULL;
    char sql3[] = "SELECT * FROM waypoints";
    int rc = sqlite3_exec(db, sql3, callback, 0, &zErrMsg);
    if( rc != SQLITE_OK )
    {
        cout << "SQL error" << zErrMsg << endl;
        sqlite3_free(zErrMsg);
    } else {
        cout << "Table created successfully";
    }

    //wildwood::Gpx gpx;

    //gpx.loadFromFile("/home/blue/in/test.gpx");

    sqlite3_close(db);
    */
}

int main(int argc, char **argv)
{
    wxInitializer initializer;
    if ( !initializer )
    {
        fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
        return -1;
    }
    wxInitAllImageHandlers();

    //testGeoUtilsBasic();
    //testGeoUtils();
    //testTileProvider();
    testHttpLib();

    return 0;
}

